﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using AmiCheckin.Lib;
using AmiCheckin.Lib.Model;
using XQ.Net.HIni.Tool;

namespace AmiCheckin.Code.Util
{
    public class DataUtil
    {

        #region Ami自带

        public static bool CanCheckin(long group, long user, string setKey)
        {
            return AmiCheckin.Config.SaveMode switch
            {
                LocationType.INI => CanCheckin_Ini(group, user, setKey),
                _ => false,
            };
        }

        private static bool CanCheckin_Ini(long group, long user, string setKey)
        {
            var path = Path.Combine(PathUtil.AppDirectory, "datas",
                (AmiCheckin.Config.GlobalMode) ? $"{user}.ini" : $"{group}\\{user}.ini");
            PathUtil.InitFile(path);
            IniObject ini = new IniObject(path);
            ini.Load();
            return ini[DateTime.Now.ToString("yyyyMMdd")][setKey] != "checked";
        }


        public static bool SetCheckin(long group, long user, string setKey)
        {
            return AmiCheckin.Config.SaveMode switch
            {
                LocationType.INI => SetCheckin_Ini(group, user, setKey),
                _ => false,
            };
        }


        private static bool SetCheckin_Ini(long group, long user, string setKey)
        {
            var path = Path.Combine(PathUtil.AppDirectory, "datas",
                (AmiCheckin.Config.GlobalMode) ? $"{user}.ini" : $"{group}\\{user}.ini");
            PathUtil.InitFile(path);
            IniObject ini = new IniObject(path);
            ini.Load();
            ini[DateTime.Now.ToString("yyyyMMdd")][setKey] = "checked";
            ini.Save();
            return true;
        }
        
        

        #endregion
        
        

        public static void AddCoin(long group, long user, CheckinSet set, out List<(string, double)> dic)
        {
            List<(string, double)> list = new();
            foreach (var reward in set.Rewards)
            {
                var rewardValue = reward.Value;
                var randomCount = RandomUtil.Number(rewardValue.Min, rewardValue.Max);
                var selected = AmiCheckin.Config.RewardTypes.First(x => x.Key == reward.Key);

                var newPath = selected.Value.Path.ReplaceWithQQandGroup(user, group);

                var opertaePath = Path.GetFullPath(selected.Value.Path);

                switch (selected.Value.LocationType)
                {
                    case LocationType.INI:
                        AddCoin_Ini(opertaePath, 
                            selected.Value.Args[0].ReplaceWithQQandGroup(user,group), 
                            selected.Value.Args[1].ReplaceWithQQandGroup(user,group), 
                            randomCount);
                        break;
                }

                list.Add((reward.Key, randomCount));
            }

            dic = list;
        }


        private static void AddCoin_Ini(string path, string sec, string key, double count)
        {
            IniObject ini = new IniObject(path);
            ini.Load();
            var old = ini[sec][key];
            ini[sec][key] = (double.Parse(old == "" ? "0" : old) + count).ToString(CultureInfo.InvariantCulture);
            ini.Save();
        }
    }
}